import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:richedit/data/audio_entity.dart';
import 'package:richedit/data/divider_entity.dart';
import 'package:richedit/data/doc_entity.dart';
import 'package:richedit/data/image_entity.dart';
import 'package:richedit/data/image_group_entity.dart';
import 'package:richedit/data/text_entity.dart';
import 'package:richedit/data/video_entity.dart';
import 'package:richedit/generated/json/base/json_convert_content.dart';
import '../Constant.dart';

class Convert{
  static entityList2String(List dataList){
    List list = [];
    for (var element in dataList) {
      switch(element.name){
        case "Text":
          list.add((element as TextEntity).toJson());
          break;
        case WidgetType.Divider:
          list.add((element as DividerEntity).toJson());
          break;
        case WidgetType.Image:
          list.add((element as ImageEntity).toJson());
          break;
        case WidgetType.Video:
          list.add((element as VideoEntity).toJson());
          break;
        case WidgetType.Audio:
          list.add((element as AudioEntity).toJson());
          break;
        case WidgetType.Doc:
          list.add((element as DocEntity).toJson());
          break;
        case WidgetType.ImageGroup:
          list.add((element as ImageGroupEntity).toJson());
          break;
      }
    }
    var json = {
      "version":"0.0.1",
      "children":list,
    };
    return jsonEncode(json);
  }

  static string2EntityList(String? data){
    Map<String, dynamic> json = {
      "version":"0.0.1",
      "children":[],
    };
    bool isEmpty = data == null || data.isEmpty;
    Map map = isEmpty ? json : jsonDecode(data);
    List dataList = [];
    map['children'].forEach((e){
      switch(e['name']){
        case WidgetType.Text:
          dataList.add(JsonConvert.fromJsonAsT<TextEntity>(e));
          break;
        case WidgetType.Divider:
          dataList.add(JsonConvert.fromJsonAsT<DividerEntity>(e));
          break;
        case WidgetType.Image:
          dataList.add(JsonConvert.fromJsonAsT<ImageEntity>(e));
          break;
        case WidgetType.Video:
          dataList.add(JsonConvert.fromJsonAsT<VideoEntity>(e));
          break;
        case WidgetType.Audio:
          dataList.add(JsonConvert.fromJsonAsT<AudioEntity>(e));
          break;
        case WidgetType.Doc:
          dataList.add(JsonConvert.fromJsonAsT<DocEntity>(e));
          break;
        case WidgetType.ImageGroup:
          dataList.add(JsonConvert.fromJsonAsT<ImageGroupEntity>(e));
          break;
      }
    });
    return dataList;
  }
  ///#FF0000类型转换成Color
  static Color parseColor(String? color){
    if (color == null){
      return Colors.black87;
    }
    if (color.length == 9){
      return Color(int.parse(color.substring(1, 7), radix: 16));
    }else{
      return Color(int.parse(color.substring(1, 7), radix: 16)+ 0xFF000000);
    }
  }
  static String color2String(Color color){
    return "#${color.value.toRadixString(16).substring(2)}";
  }
}